package com.aboo.infrastructure.aop.frame.aop;

import com.aboo.infrastructure.aop.frame.annoation.AnnotationAop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @ClassName RequestAop
 * @Description
 * @Author louan
 * @Date 2021/3/22
 */
@Slf4j
@Aspect
@Order(value = 1)
@Component
public class AnnotationAspect {

    /**
     * 前置通知, 在方法执行之前执行
     **/
    @Before(value = "@annotation(annotationAop)")
    public void before(JoinPoint joinPoint, AnnotationAop annotationAop) {
        log.info("开始AnnotationAspect的before，内容:{}", annotationAop.desc());
    }

    /**
     * 后置通知, 在方法执行之后执行
     **/
    @After(value = "@annotation(annotationAop)")
    public void after(JoinPoint joinPoint, AnnotationAop annotationAop) {
        log.info("开始AnnotationAspect的after，内容:{}", annotationAop.desc());
    }

    /**
     * 返回通知, 在方法返回结果之后执行
     **/
    @AfterReturning(value = "@annotation(annotationAop)")
    public void afterReturning(JoinPoint joinPoint, AnnotationAop annotationAop) {
        log.info("开始AnnotationAspect的afterReturning，内容:{}", annotationAop.desc());
    }

    /**
     * 异常通知, 在方法抛出异常之后
     **/
    @AfterThrowing(value = "@annotation(annotationAop)", throwing = "throwable")
    public void afterThrowing(JoinPoint joinPoint, AnnotationAop annotationAop, Throwable throwable) {
        log.info("开始AnnotationAspect的afterThrowing，内容:{}", annotationAop.desc());
    }

    /**
     * 环绕通知, 围绕着方法执行
     **/
    @Around(value = "@annotation(annotationAop)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint, AnnotationAop annotationAop) throws Throwable {
        log.info("开始AnnotationAspect的around，内容:{}", annotationAop.desc());
        Object result = proceedingJoinPoint.proceed();
        log.info("结束AnnotationAspect的around，内容:{}", annotationAop.desc());
        return result;
    }

}
